using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Contact_Manager
{
    public partial class Export : Form
    {
        public string Format
        {
            get { return txtFormat.Text; }
            set { txtFormat.Text = value; }
        }

        public Export()
        {
            InitializeComponent();
        }
        
        private void Export_Load(object sender, EventArgs e)
        {
            txtFormat.Text = this.Format;

            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Export_KeyDown);
        }

        private void Export_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape) this.Close();
            else if (e.KeyData == Keys.Enter) this.ExportOutput();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog(this) == DialogResult.OK)
            {
                txtFile.Text = fileDialog.FileName;
            }
        }

        private void radFile_CheckedChanged(object sender, EventArgs e)
        {
            this.txtFile.Enabled = radFile.Checked;
            this.btnBrowse.Enabled = radFile.Checked;
        }

        private void ExportOutput()
        {
            this.Format = txtFormat.Text;
            StringBuilder sb = new StringBuilder();
            ArrayList contacts = ((ContactManager)this.Owner).Contacts;
            ArrayList curContacts = ((ContactManager)this.Owner).CurContacts;
            string delimit = Properties.Settings.Default.Delimiters;
            for (int i = 0; i < curContacts.Count; i++)
            {
                Contact cont = (Contact)contacts[(int)curContacts[i]];
                string s = Format.ToLower();

                s = s.Replace("<name>", cont.getName());

                s = s.Replace("<email>", cont.getEmail());
                s = s.Replace("<emails>", cont.getEmails(delimit));

                s = s.Replace("<phone>", cont.getPhone());
                s = s.Replace("<phones>", cont.getPhones(delimit));

                s = s.Replace("<description>", cont.getDescript());
                s = s.Replace("<descriptions>", cont.getDescripts(delimit));

                sb.AppendLine(s);
            }

            if (radClip.Checked)
            {
                bool success = true;
                do
                {
                    try
                    {
                        Clipboard.Clear();
                        Clipboard.SetText(sb.ToString());
                    }
                    catch 
                    { 
                        //success = false; 
                    }
                }
                while (!success);
            }
            else
            {
                try
                {
                    StreamWriter fw = new StreamWriter(txtFile.Text);
                    fw.Write(sb.ToString());
                    fw.Close();
                }
                catch { }
            }
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.ExportOutput();
        }

        private void Export_FormClosed(object sender, FormClosedEventArgs e)
        {
            ((ContactManager)this.Owner).Format = this.Format;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.Format = "<Name>, <Emails>, <Phones>, <Descriptions>";
        }

    }
}